package com.bb.blog.security.controller;

import com.bb.blog.security.dao.ClientDetailsMapper;
import com.bb.blog.security.model.BbClientDetails;
import com.bb.blog.web.component.RandomCodeGenerator;
import com.bb.blog.web.component.ResponseUtil;
import com.bb.blog.web.model.CommonResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * todo  添加删除 禁用
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/client")
public class ClientController {
    private final ClientDetailsMapper clientDetailsMapper;

    @PostMapping
    public CommonResponse register(@RequestBody BbClientDetails bbClientDetails) {

        String secretKey = RandomCodeGenerator.randomCode(30);
        bbClientDetails.setClientSecret(secretKey);
        clientDetailsMapper.register(bbClientDetails);
        return ResponseUtil.success(bbClientDetails);
    }

    @GetMapping("/list")
    public CommonResponse list(@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                               @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                               @RequestParam(name = "key", defaultValue = "key") String key) {
        PageHelper.startPage(pageNum, pageSize);
        List<BbClientDetails> clientDetails = clientDetailsMapper.selectList();
        PageInfo<BbClientDetails> page = new PageInfo<>(clientDetails);
        return ResponseUtil.success(page);
    }


}
